package com.ssps.back.manage.permission;

import com.ssps.entity.Permission;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MyUtils {
    public static List<Permission> getTree(List<Permission> nodes) {
        Map<Integer, Permission> map = new HashMap<>();
        //找出所有的根节点
        List<Permission> roots = new ArrayList<>();
        for (Permission node : nodes) {
            map.put(node.getPmid(), node);
        }
        for (Permission node : nodes) {
            Permission temp = map.get(node.getParentid());
            if (temp != null) {
                temp.getChildren().add(node);
            } else {
                node.setOpen(true);
                roots.add(node);
            }
        }
        return roots;
    }
}
